package utils

import (
	"crypto/tls"
	"fmt"
	"github.com/jordan-wright/email"
	"math/rand"
	"net/smtp"
	"time"
)

// MailSendCode 发送邮箱验证码
func MailSendCode(mail string, code string) error {
	e := email.NewEmail()
	//发送者邮箱
	e.From = "牛马云盘 <2458882580@qq.com>"
	//接收者邮箱
	e.To = []string{mail}
	//主题
	e.Subject = "牛马云盘-验证码"
	e.HTML = []byte("你的验证码为<h1>" + code + "</h1>")
	err := e.SendWithTLS("smtp.qq.com:465", smtp.PlainAuth("", "2458882580@qq.com", "jexrcgqljtphdieb", "smtp.qq.com"),
		&tls.Config{InsecureSkipVerify: true, ServerName: "smtp.qq.com"})
	if err != nil {
		fmt.Println(err)
		return err
	}
	return nil
}

var EmailCodeExpire = 600

// RandCode 生成验证码
func RandCode() string {
	rand.Seed(time.Now().UnixNano())
	code := ""
	for i := 0; i < 4; i++ {
		num := rand.Intn(10)
		code += fmt.Sprint(num)
	}
	return code
}
